//
// Description: 5847. 找到所有的农场组
// Created by Loading on 2021/9/5.
//

#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> findFarmland(vector<vector<int>> &land) {
    int m = land.size();
    int n = land[0].size();

    vector<vector<int>> res;

    function<void(int, int, vector<int> &)> dfs = [&](int x, int y, vector<int> &v) {
        if (land[x][y] != 1) {
            return;
        }
        // 起始坐标
        if (v.empty()) {
            v.emplace_back(x);
            v.emplace_back(y);
        }
        // 到达结尾
        if ((x + 1 == m || land[x + 1][y] == 0) && (y + 1 == n || land[x][y + 1] == 0)) {
            v.emplace_back(x);
            v.emplace_back(y);
        }
        // 遍历过的置为 2
        land[x][y] = 2;
        // 向下遍历
        if (x + 1 < m) {
            dfs(x + 1, y, v);
        }
        // 向右遍历
        if (y + 1 < n) {
            dfs(x, y + 1, v);
        }
    };

    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            vector<int> v;
            dfs(i, j, v);
            if (!v.empty()) {
                res.emplace_back(v);
            }
        }
    }

    return res;
}

int main() {
    vector<vector<int>> land = {{1, 0, 0},
                                {0, 1, 1},
                                {0, 1, 1}};
    vector<vector<int>> res = findFarmland(land);
    for (auto &v : res) {
        printf("[%d,%d,%d,%d]\n", v[0], v[1], v[2], v[3]);
    }

    return 0;
}